import api from '../../../api/index'
import dayjs from 'dayjs'
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showjl: false,
    num: null,
    show: false,
    curDate: dayjs().format('YYYY-MM-DD'),
    curTempDate: new Date().getTime(),
    loading: false,
    formatter(type, value) {
      if (type === 'year') {
        return `${value}`;
      }
      if (type === 'month') {
        return `${value}`;
      }
      return value;
    },
    showdelete: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var { curDate } = options
    if (curDate) {
      this.setData({
        curDate
      })
      const date = new Date(curDate);
      const daysOfWeek = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
      const dayOfWeek = daysOfWeek[date.getDay()];
      console.log(dayOfWeek);
      this.setData({
        dayOfWeek
      })
    } else {
      const date = new Date(this.data.curDate);
      const daysOfWeek = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
      const dayOfWeek = daysOfWeek[date.getDay()];
      console.log(dayOfWeek);
      this.setData({
        dayOfWeek
      })
    }
    this.getlistdate(this.data.curDate)
  },
  onShow: function () {
    console.log(this.data.curDate);
    const date = new Date(this.data.curDate);
      const daysOfWeek = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
      const dayOfWeek = daysOfWeek[date.getDay()];
      console.log(dayOfWeek);
      this.setData({
        dayOfWeek
      })
    this.getlistdate(this.data.curDate)
  },
  onInput(event) {
    this.setData({
      curTempDate: event.detail,
    });
  },
  onClose() {
  },
  confirmDate() {
    const datea = this.data.curDate
    const dateb = dayjs(this.data.curTempDate).format('YYYY-MM-DD')
    if (datea == dateb) {
      return
    }
    this.setData({
      curDate: dayjs(this.data.curTempDate).format('YYYY-MM-DD')
    })
    const date = new Date(this.data.curDate);
    const daysOfWeek = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
    const dayOfWeek = daysOfWeek[date.getDay()];
    console.log(dayOfWeek);
    this.setData({
      dayOfWeek
    })
    this.getlistdate(this.data.curDate)
  },
  selDate: function (evt) {
    this.setData({
      curTempDate: dayjs(this.data.curDate).valueOf(),
      show: true
    })
  },
  getlistdate(curDate) {
    api.post('/api/hxhealth/sportrecord/list', {
      data: {
        "date": curDate
      }
    }).then(res => {
      console.log(res);
      // console.log(JSON.stringify(res));
      if (res.length > 0) {
        let data = res
        let totalKcal = 0;
        for (let i = 0; i < data.length; i++) {
          totalKcal += parseFloat(data[i].kcal);
        }
        console.log('kcal总和：', totalKcal);
        console.log(res);
        this.setData({
          itemData: res,
          showjl: true,
          totalKcal: totalKcal.toFixed(3)
        })
      } else {
        this.setData({
          itemData: [],
          showjl: false,
          totalKcal: 0
        })
      }
    }).catch(e => {
      console.log(e)
    })
  },
  delete(e) {
    console.log(e.currentTarget.dataset.id);
      let id = e.currentTarget.dataset.id
    var that = this
    Dialog.confirm({
      title: '删除',
      message: '确定要删除吗？',
    })
      .then(() => {
        console.log('2222');
        that.setData({
          itemData: [],
          totalKcal: 0
        })
        try {
          api.get(`/api/hxhealth/sportrecord/remove/${id}`).then((res) => {
            console.log(res);
            that.getlistdate(that.data.curDate)
          });
        } catch (e) {
          console.error(e);
        }
      })
      .catch(() => {
        // on cancel
        console.log('333');
      });

  },
  handleTap() {
    console.log(this.data.curDate);
    wx.navigateTo({
      url: `/dietaryRecommendations/pages/sportsrecordlist/index?time=${this.data.curDate}`
    });
  },
  getUserInfo(event) {
    console.log(event.detail);
  },

  onClose() {
    this.setData({ showdelete: false });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})